var crud = require('../../util/crud');
var model = require('../../model/index.js');
var Teacher = model.get('Teacher');
var response = require('../../util/response');
var modules = {};

modules.create = (req, res) => {
   
    Teacher.create(req.body).then((teacher) => {

    	return res.json(response.success({data: teacher}))
    }).catch(err => {
        console.log('teacher create err, err = ' + err);
        return res.json(response.error('新建老师失败')).end();
    })
}
modules.find = (req, res) => {
    const query = req.query,
        filter = query.filter || {},
        limit = query.limit,
        skip = query.skip;
    
    Teacher.find(filter).limit(limit).skip(skip).then((teachers) => {

    	return res.json(response.success({data: teachers}))
    }).catch(err => {
        console.log('teacher find err, err = ' + err);
        return res.json(response.error('查找老师失败')).end();
    })
}
modules.update = (req, res) => {
    const filter = req.query;
    const update = req.body;

    Teacher.update(filter, update).then((teacher) => {
    	return res.json(response.success({message: '更新老师成功', data: teacher}))
    }).catch(err => {
        console.log('teacher update err, err = ' + err);
        return res.json(response.error('更新老师失败')).end();
    })
}
modules.remove = (req, res) => {
    const filter = req.query;

    Teacher.remove(filter).then((result) => {
    	return res.json(response.success({message: '删除老师成功'}))
    }).catch(err => {
        console.log('teacher remove err, err = ' + err);
        return res.json(response.error('删除老师失败')).end();
    })
}

module.exports = (router) => {
    crud(router, modules);
}